package com.ebix.sample.controller;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.ebix.sample.dao.AdminDAO;
import com.ebix.sample.excetion.WisException;
import com.ebix.sample.to.CorretorTO;
import com.ebix.sample.to.UserTO;

/**
 * this facade adds the user in the session. we also get that user in the
 * Javascript side to fill some data
 * 
 * @author Ebix L.A.
 * 
 */
public class AcessoFacade {

	public UserTO login(UserTO user) throws WisException {
		String thm = user.getTheme();
		// no special logic, so let's go directly to the DAO
		user = new AdminDAO().findUser(user);
		if (!"".equals(thm))
			user.setTheme(thm);
		WebContext ctx = WebContextFactory.get();
		ctx.getSession().setAttribute("user", user);

		CorretorTO corretor = new CorretorTO();
		corretor.setId(1);
		corretor.setFooCode("bar");
		user.setCorretor(corretor);

		return user;
	}

	public void logout() throws WisException {
		WebContext ctx = WebContextFactory.get();
		ctx.getSession().removeAttribute("user");
		ctx.getSession().invalidate();
	}

	public UserTO getCurrentUser() throws WisException {
		WebContext ctx = WebContextFactory.get();
		UserTO user = (UserTO) ctx.getSession().getAttribute("user");
		return user;
	}
}
